__author__ = 'Muscle'
__author__ = 'Muscle'

from mui import *
from mui.Widgets import *
import time
import mui.color as color

class MyWindow(MUIWindow):
    def __init__(self, **kwargs):
        MUIWindow.__init__(self, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'
        
        self._stackpanel = MUIStackPanel(self, left = 100, top = 50, width = 200,height = 200, bgColor = color.red)
        self._stackpanel.name = 'vStackPanel'
        for i in range(0, 10):
            btnText = "btn " + str(i)
            o = MUITextButton(self._stackpanel, left = 0, top = 0, width = 120, height = 100, text = btnText)

        self.scrollDownBtn = MUITextButton(self, left = 400, top = 50, width = 60, height = 40, text = 'onScrollDown')
        self.scrollUpBtn = MUITextButton(self, left = 400, top = 100, width = 60, height = 40, text = 'onScrollUp')
        self.scrollDownBtn.bind('Mouse Up', self.on_btnScrollDown_click)
        self.scrollUpBtn.bind('Mouse Up', self.on_btnScrollUp_click)
        
        #Horizontal StackPanel
        self._hstackpanel = MUIStackPanel(self, left = 100, top = 400, width = 200,height = 200, orientation = 'Horizontal', bgColor = color.blue)
        for i in range(0, 20):
            btnText = "btn " + str(i)
            o2 = MUITextButton(self._hstackpanel, left = 0, top = 0, width = 50, height = 50, text = btnText)
          

        self.hscrollDownBtn = MUITextButton(self, left = 400, top = 400, width = 60, height = 40, text = 'onHScrollDown')
        self.hscrollUpBtn = MUITextButton(self, left = 400, top = 450, width = 60, height = 40, text = 'onHScrollUp')
        self.hscrollDownBtn.bind('Mouse Up', self.on_btnHScrollDown_click)
        self.hscrollUpBtn.bind('Mouse Up', self.on_btnHScrollUp_click)
        


        # for FPS

        self.__fpsCount = 0
        self.__fpsTimeStart = 0;
        self.__fosTimeEnd = 0;
        self.fpsText = MUIText(self, right = self.width-20, top = 20, width = 200, height = 40, text = "FPS: 0", fontSize = 20, fontColor = color.blue, fontWeight = 'Bold')
        self.bindData('width', self.fpsText, 'right', dir = '->', converter =  lambda x: x-20 )

    def on_btnScrollDown_click(self, btn, _x, _y, buttons, modifiers):
        self._stackpanel.onScroll(min(self._stackpanel.scrollTarget + 50, self._stackpanel.fullSize))

    def on_btnScrollUp_click(self, btn, _x, _y, buttons, modifiers):
        self._stackpanel.onScroll(max(self._stackpanel.scrollTarget - 50, 0))

    def on_btnHScrollDown_click(self, btn, _x, _y, buttons, modifiers):
        self._hstackpanel.onScroll(min(self._hstackpanel.scrollTarget + 50, self._hstackpanel.fullSize))

    def on_btnHScrollUp_click(self, btn, _x, _y, buttons, modifiers):
        self._hstackpanel.onScroll(max(self._hstackpanel.scrollTarget - 50, 0))


    def on_btn1_click(self, btn, _x, _y, buttons, modifiers):
        print 'btn 1 click!!'

    def on_btn2_click(self, btn, _x, _y, buttons, modifiers):
        print 'btn 2 click!!'

    def on_btn3_click(self, btn, _x, _y, buttons, modifiers):
        print 'btn 3 click!!'

    def on_btn4_click(self, btn, _x, _y, buttons, modifiers):
        print 'btn 4 click!!'

    def on_paint(self):
        if (self.__fpsCount == 0):
            self.__fpsTimeStart = time.clock()
        super(MyWindow, self).on_paint()

        self.__fpsCount += 1

        if (self.__fpsCount >= 120):
            self.__fpsTimeEnd = time.clock()
            diffTime = self.__fpsTimeEnd - self.__fpsTimeStart
            fps = self.__fpsCount / diffTime
            self.fpsText.text = "FPS: " + str(int(fps))
            self.__fpsCount = 0
        


def main():
    mui.muiInit(main)
    g_DXDevice = muiDX.CDXDevice()
    w = MyWindow(width = 1024, height = 768, dxDevice = g_DXDevice)
    w.ShowWindow(1)
    muiApp.run()
    g_DXDevice.Release()
    mui.muiUnInit()



main()
